<template>
  <ContentWrap>
    <!-- 搜索工作栏 -->
    <el-form
      class="-mb-15px"
      :model="queryParams"
      ref="queryFormRef"
      :inline="true"
      label-width="94px"
    >
      <el-form-item label="操作人名称" prop="userName">
        <el-input
          v-model="queryParams.userName"
          placeholder="请输入操作人名称"
          clearable
          @keyup.enter="handleQuery"
          class="!w-280px"
        />
      </el-form-item>
      <el-form-item label="操作类型" prop="type">
        <el-select
          v-model="queryParams.type"
          placeholder="请选择操作类型"
          clearable
          class="!w-240px"
        >
          <el-option label="未审核通过" value="未审核通过" />
          <el-option label="审核通过" value="审核通过" />
          <el-option label="已关闭" value="已关闭" />
          <el-option label="编辑" value="编辑" />
          <el-option label="已完成" value="已完成" />
        </el-select>
      </el-form-item>
      <el-form-item label="操作时间" prop="createTime">
        <el-date-picker
          v-model="queryParams.createTime"
          value-format="YYYY-MM-DD HH:mm:ss"
          type="daterange"
          start-placeholder="开始日期"
          end-placeholder="结束日期"
          :default-time="[new Date('1 00:00:00'), new Date('1 23:59:59')]"
          class="!w-220px"
        />
      </el-form-item>
      <el-form-item>
        <el-button @click="handleQuery"><Icon icon="ep:search" class="mr-5px" /> 搜索</el-button>
        <el-button @click="resetQuery"><Icon icon="ep:refresh" class="mr-5px" /> 重置</el-button>
        <el-button
          type="primary"
          plain
          @click="openForm('create')"
          v-hasPermi="['dd:log:create']"
        >
          <Icon icon="ep:plus" class="mr-5px" /> 新增
        </el-button>
        <el-button
          type="success"
          plain
          @click="handleExport"
          :loading="exportLoading"
          v-hasPermi="['dd:log:export']"
        >
          <Icon icon="ep:download" class="mr-5px" /> 导出
        </el-button>
      </el-form-item>
    </el-form>
  </ContentWrap>

  <!-- 列表 -->
  <ContentWrap>
    <el-table v-loading="loading" :data="list" :stripe="true" :show-overflow-tooltip="true">
      <el-table-column label="序号" align="center" width="80px">
      <template #default="scope">
        {{ serialNumbers[scope.$index] }}
      </template>
    </el-table-column>
      <el-table-column label="操作人名称" align="center" prop="userName" />
      <el-table-column label="操作类型" align="center" prop="type" />
      <el-table-column label="需求编号" align="center" prop="demandNum" />
      <el-table-column
        label="操作时间"
        align="center"
        prop="createTime"
        :formatter="dateFormatter"
        width="180px"
      />
      <el-table-column label="操作内容" align="center" min-width="120px">
        <template #default="scope">
          <el-button
            link
            type="success"
            @click="openForm('update', scope.row.id, scope.row)"
            v-if="scope.row.type !== '未审核通过' && scope.row.type !== '已关闭' && scope.row.type !== '已完成'"
          >
            详情
          </el-button>
          <span v-if="scope.row.type === '未审核通过'"> {{ JSON.parse(scope.row.content).notPassing }}</span>
          <span v-if="scope.row.type === '已关闭'"> ---------</span>
          <span v-if="scope.row.type === '已完成'"> ---------</span>
          </template>
      </el-table-column>
    </el-table>
    <!-- 分页 -->
    <Pagination
      :total="total"
      v-model:page="queryParams.pageNo"
      v-model:limit="queryParams.pageSize"
      :page-sizes="[10, 20, 50]"
      @pagination="getList"
    />
  </ContentWrap>
</template>

<script setup lang="ts">
import { dateFormatter } from '@/utils/formatTime'
import download from '@/utils/download'
import { LogApi, LogVO } from '@/api/dd/log'
import {usePermissionStore} from "@/store/modules/permission";
/** 操作日志 列表 */
defineOptions({ name: 'Log' })

const message = useMessage() // 消息弹窗
const { t } = useI18n() // 国际化

const loading = ref(true) // 列表的加载中
const list = ref<LogVO[]>([]) // 列表的数据
const total = ref(0) // 列表的总页数
const queryParams = reactive({
  pageNo: 1,
  pageSize: 10,
  userName: undefined,
  type: undefined,
  createTime: [],
})
const queryFormRef = ref() // 搜索的表单
const exportLoading = ref(false) // 导出的加载中

const serialNumbers = ref<number[]>([])

/** 查询列表 */
const getList = async () => {
  loading.value = true
  try {
    const data = await LogApi.getLogPage(queryParams)
    list.value = data.list
    total.value = data.total
     // 生成序号数组
     generateSerialNumbers()
  } finally {
    loading.value = false
  }
}

/** 搜索按钮操作 */
const handleQuery = () => {
  queryParams.pageNo = 1
  getList()
}

/** 重置按钮操作 */
const resetQuery = () => {
  queryFormRef.value.resetFields()
  handleQuery()
}

/** 添加/修改操作 */
const formRef = ref()
const mainId = ref('')

const permissionStore = usePermissionStore()

const router = useRouter()

const openForm = (type: string, id?: number, row: any) => {
  const data = JSON.parse(row.content)
  console.log(data, 'data asdf')
  // formRef.value.open(type, id, data)

  permissionStore.setDoLog(data)

  nextTick(() => {

    router.push({
      name: 'DialogDoLogInfoMainForm',
      query: {
        type,
        id,
        mainId: data.id,
        status: data.status
      }
    })

  })
}


/** 导出按钮操作 */
const handleExport = async () => {
  try {
    // 导出的二次确认
    await message.exportConfirm()
    // 发起导出
    exportLoading.value = true
    const data = await LogApi.exportLog(queryParams)
    download.excel(data, '操作日志.xls')
  } catch {
  } finally {
    exportLoading.value = false
  }
}
// 生成序号数组
const generateSerialNumbers = () => {
  const startIndex = (queryParams.pageNo - 1) * queryParams.pageSize + 1
  serialNumbers.value = list.value.map((_, index) => startIndex + index)
}

// 监听分页参数变化，重新生成序号
watch([() => queryParams.pageNo, () => queryParams.pageSize], () => {
  generateSerialNumbers()
})
/** 初始化 **/
onMounted(() => {
  getList()
})
</script>